package fhq.hcmute.edu.vn.activities;

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import fhq.hcmute.edu.vn.message.MessageFactory;
import fhq.hcmute.edu.vn.message.MyMessage;
import fhq.hcmute.edu.vn.network.Network;

public class PreziActivity extends Activity {

	private TextView btnNext;
	private TextView btnPrevious;
	private TextView btnPause;
	private TextView btnScreen;
	private boolean isFullScreen = false;
	private Network network;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_prezi);
		btnNext = (TextView) findViewById(R.id.btnNext);
		btnNext.setOnClickListener(onClickNext);

		btnPrevious = (TextView) findViewById(R.id.btnPrevious);
		btnPrevious.setOnClickListener(onClickPrevious);

		btnPause = (TextView) findViewById(R.id.btnPause);
		btnPause.setOnClickListener(onClickPause);

		btnScreen = (TextView) findViewById(R.id.btnScreen);
		btnScreen.setOnClickListener(onClickScreen);
		network = Network.getInstance();
		
		getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	private View.OnClickListener onClickNext = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			MyMessage message = MessageFactory.createPreziNextMessage();
			try {
				network.sendToServer(message);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	private View.OnClickListener onClickPrevious = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			MyMessage message = MessageFactory.createPreziBackMessage();
			try {
				network.sendToServer(message);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	private View.OnClickListener onClickPause = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			MyMessage message = MessageFactory.createPreziBlackOutMessage();
			try {
				network.sendToServer(message);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	private View.OnClickListener onClickScreen = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			isFullScreen = btnScreen.isSelected();
			btnScreen.setSelected(!isFullScreen);
			MyMessage message = null;
			if (isFullScreen) {
				message = MessageFactory.createPreziExitFullScreenMessage();
			} else {
				message = MessageFactory.createPreziEnterFullScreenMessage();
			}
			try {
				network.sendToServer(message);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	
	@Override
	protected void onDestroy() {
		getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		super.onDestroy();
	}
}
